﻿using System;
using System.IO;
using System.Text;

namespace SFL
{
    public static class WriteLog
    {
        #region 方法--写错误日志
        /// <summary>
        /// 写错误日志
        /// </summary>
        public static void WriteErrLog(string errTitle, Exception ex=null)
        {
            var dir = Directory.GetCurrentDirectory();
            var filePath = Path.Combine(dir, "Log");
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            var fileName = Path.Combine(filePath, DateTime.Now.ToString("yyyyMMdd")+ "_网站监控错误日志.txt");
                        

            StringBuilder strBuilderErrorMessage = new StringBuilder();

            strBuilderErrorMessage.Append("-------------------------------------------------------------------------\r\n");
            strBuilderErrorMessage.Append("日期:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
            strBuilderErrorMessage.Append("错误标题:" + errTitle + "\r\n");
            strBuilderErrorMessage.Append("错误信息:" + ex.Message + "\r\n");
            strBuilderErrorMessage.Append("错误跟踪:" + ex.StackTrace + "\r\n");
            strBuilderErrorMessage.Append("-------------------------------------------------------------------------\r\n");
            try
            {
                using (StreamWriter sw = File.AppendText(fileName))
                {
                    sw.Write(strBuilderErrorMessage);
                    sw.Flush();
                }
            }
            catch (Exception exx)
            {
                File.AppendAllText(fileName, $"日志写入异常：{exx.ToString()}" + "\r\n");
            }
    }
        #endregion

        #region 方法--写工作日志
        /// <summary>
        /// 写日志
        /// </summary>
        public static void WriteWorkLog(string title, string content)
        {
            var dir = Directory.GetCurrentDirectory();
            var filePath = Path.Combine(dir, "Log");
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            var fileName = Path.Combine(filePath, DateTime.Now.ToString("yyyyMMdd") + "_网站监控工作日志.txt");
            
            StringBuilder strBuilderErrorMessage = new StringBuilder();

            strBuilderErrorMessage.Append("-------------------------------------------------------------------------\r\n");
            strBuilderErrorMessage.Append("日期:" + System.DateTime.Now.ToString() + "\r\n");
            strBuilderErrorMessage.Append("标题:" + title + "\r\n");
            strBuilderErrorMessage.Append("内容:" + content + "\r\n");
            strBuilderErrorMessage.Append("-------------------------------------------------------------------------\r\n");
            try { 
                using (StreamWriter sw = File.AppendText(fileName))
                {
                    sw.Write(strBuilderErrorMessage);
                    sw.Flush();
                }
            }
            catch (Exception exx)
            {
                File.AppendAllText(fileName, $"日志写入异常：{exx.ToString()}" + "\r\n");
            }
        }
        #endregion
    }
}
